package com.xlf.service.ai.service.impl;

import com.xlf.common.redis.service.RedisCache;
import com.xlf.service.ai.pojo.domain.AiRecord;
import com.xlf.service.ai.pojo.dto.Message;
import com.xlf.service.ai.service.MessagesContainer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @Author xlf 2734029211@qq.com
 * @Date 2024/1/29 19:37
 * @Description: TODO 使用redis做为消息的缓存
 * @Version 1.0
 */
@Service
public class RedisMessagesContainer implements MessagesContainer {

    @Autowired
    RedisCache redisCache;

    private static String AI =  "AiList:";

    @Override
    public List<Message> getMessagesHistory(String msgId) {

        String key = AI + msgId;
        List<AiRecord> cacheList = redisCache.getCacheList(key);

        return Optional.ofNullable(cacheList).orElse(new ArrayList<AiRecord>())
                .stream().map(item -> item.getMessage()).collect(Collectors.toList());
    }

    @Override
    public void addMessagesHistory(Message message, String msgId) {
        AiRecord aiRecord = new AiRecord();
        aiRecord.setMessage(message);
        aiRecord.setCreateTime(new Date());
        String key = AI + msgId;
        redisCache.addCacheList(key,aiRecord);
    }

    @Override
    public void setMessagesHistory(List<Message> messages, String msgId) {

    }

    @Override
    public void cleanMessages(String msgId) {

    }
}
